package com.qys.livesMall.app.user.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qys.livesMall.app.user.constants.TencentCloudImApiConstant;
import com.qys.livesMall.app.user.constants.TencentCloudImConstant;
import com.qys.livesMall.app.user.mapper.H2AppUserMapper;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.kafka.config.YamlPropertySourceFactory;
import com.tencentyun.TLSSigAPIv2;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Component
@PropertySource(value = "classpath:config/spring-im.yml", factory = YamlPropertySourceFactory.class)
public class TencentCloudIm {
    private static final String REDIS_IM_USER_SIG = "silence:im_user_sig:";

    @Value("${silence.tencent.cloud.im.sdkAppId}")
    private long sdkAppId;

    @Value("${silence.tencent.cloud.im.key}")
    private String key;
    /**
     * 管理员账号
     */
    @Value("${silence.tencent.cloud.im.identifier}")
    private String identifier;

    @Value("${silence.tencent.cloud.im.httpsUrlPrefix}")
    private String httpsUrlPrefix;

    @Resource
    private RedisCache redisCache;

    @Resource
    private H2AppUserMapper h2AppUserMapper;

    /**
     * 获取腾讯云用户签名(管理员)
     */
    public String getTxCloudUserSig() {
        String userSig = redisCache.getCacheObject(REDIS_IM_USER_SIG + identifier);
        if (StringUtils.isEmpty(userSig)) {
            TLSSigAPIv2 tlsSigApi = new TLSSigAPIv2(sdkAppId, key);
            userSig = tlsSigApi.genUserSig(identifier, 86400);
            redisCache.setCacheObject(REDIS_IM_USER_SIG + identifier, userSig, 86400L, TimeUnit.SECONDS);
        }
        return userSig;
    }

    /**
     * 获取腾讯云用户签名
     */
    public String getTxCloudCustomerSig(String userId) {
        String userSig = redisCache.getCacheObject(REDIS_IM_USER_SIG + userId);
        if (StringUtils.isEmpty(userSig)) {
            TLSSigAPIv2 tlsSigApi = new TLSSigAPIv2(sdkAppId, key);
            userSig = tlsSigApi.genUserSig(userId, 86400);
            redisCache.setCacheObject(REDIS_IM_USER_SIG + userId, userSig, 86400L, TimeUnit.SECONDS);
        }
        return userSig;
    }


    /**
     * 获取腾讯im请求路径
     */
    private String getHttpsUrl(String imServiceApi, Integer random) {
        return String.format("%s%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=json",
                httpsUrlPrefix, imServiceApi, sdkAppId, identifier, this.getTxCloudUserSig(), random);
    }

    /**
     * 导入单个账号
     *
     * @param userId
     * @param nickName
     * @param faceUrl
     */
    public void accountImport(String userId, String nickName, String faceUrl) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_IMPORT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserID", userId);
        if (StringUtils.isNotEmpty(nickName)) {
            jsonObject.put("Nick", nickName);
        }
        if (StringUtils.isNotEmpty(faceUrl)) {
            jsonObject.put("FaceUrl", faceUrl);
        }
        log.info("腾讯云im导入单个账号，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im导入单个账号，返回结果：{}", result);
    }

    /**
     * 导入多个账号
     *
     * @param userIds 用户id集合
     */
    public void multiAccountImport(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.MULTI_ACCOUNT_IMPORT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Accounts", userIds);
        log.info("腾讯云im导入多个账号，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im导入单个账户，返回结果：{}", result);
    }

    /**
     * 查询账号是否已经导入im
     *
     * @param userIds 用户id集合
     */
    public Map<String, Boolean> accountCheck(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_CHECK, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("CheckItem", getUserIdJsonList(userIds));
        log.info("腾讯云im查询账号，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im查询账号，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        List<JSONObject> resultItem = resultJson.getList("ResultItem", JSONObject.class);
        return resultItem.stream().collect(Collectors.toMap(m -> m.getString("UserID"),
                n -> StringUtils.equalsAnyIgnoreCase("Imported", n.getString("AccountStatus"))));
    }

    private List<JSONObject> getUserIdJsonList(List<String> userIds) {
        return userIds.stream().map(v -> {
            JSONObject userIdJson = new JSONObject();
            userIdJson.put("UserID", v);
            return userIdJson;
        }).collect(Collectors.toList());
    }

    /**
     * 删除账号
     *
     * @param userIds 用户id集合
     */
    public void accountDelete(List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_DELETE, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("DeleteItem", getUserIdJsonList(userIds));
        log.info("腾讯云im删除账号，请求参数：{}", jsonObject.toString());
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im删除账户，返回结果：{}", result);
    }

    /**
     * 设置资料，修改头像
     */
    public String updateUser(String userId, String avatar, String nickName) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.PortraitManage.PORTRAIT_SET, random);
        // 上传完成之后修改IM用户的头像
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> avatarMap = new HashMap<>();
        Map<String, String> nickMap = new HashMap<>();
        // 待设置的资料字段的值
        if (StringUtils.isNotBlank(avatar)) {
            //指定要设置的资料字段的名称
            avatarMap.put("Tag", "Tag_Profile_IM_Image");
            avatarMap.put("Value", avatar);
            list.add(avatarMap);
        }
        //指定要设置的资料字段的名称
        if (StringUtils.isNotBlank(nickName)) {
            nickMap.put("Tag", "Tag_Profile_IM_Nick");
            // 待设置的资料字段的值
            nickMap.put("Value", nickName);
            list.add(nickMap);
        }
        JSONObject obj = new JSONObject();
        obj.put("From_Account", userId);
        obj.put("ProfileItem", list);
        log.info("腾讯云im更新账号，请求参数：{}", obj);
        String result = HttpUtil.doPost2(httpsUrl, obj);
        log.info("腾讯云im更新账号，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);

        return resultJson.getString("ActionStatus");
    }

    /**
     * 创建群组并添加群成员
     *
     * @param userIds
     * @return
     */
    public String createGroup(String groupName,List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.CREATE_GROUP, random);
        JSONObject obj = new JSONObject();

        // 群组类型：Private/Public/ChatRoom/AVChatRoom/Community
        obj.put("Type", "AVChatRoom");
        // 群名称（必填）
        obj.put("Name", groupName);
        // 群简介（选填）
        obj.put("Introduction", "");
        // 群公告（选填）
        obj.put("Notification", "");
        // 群头像 URL（选填）
        obj.put("FaceUrl", "");
        // 申请加群处理方式（选填）
        obj.put("ApplyJoinOption", "FreeAccess");
        //邀请加群处理方式(选填)
        obj.put("InviteJoinOption", "FreeAccess");
        log.info("腾讯云im创建群，请求参数：{}", obj);
        String result = HttpUtil.doPost2(httpsUrl, obj);
        log.info("腾讯云im创建群，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if (CollectionUtils.isNotEmpty(userIds) &&
                StringUtils.equalsAnyIgnoreCase("OK", resultJson.getString("ActionStatus"))) {
//            添加群成员
            addGroupMember( resultJson.getString("GroupId"),userIds);
        }
        return resultJson.getString("GroupId");
    }
    /**
     * 解散群聊
     * @param groupId
     * @return
     */
    public String destroyGroup(String groupId){
        //            添加群成员
        Integer addRandom = RandomUtils.nextInt(0, 999999999);
        String addHttpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.DESTROY_GROUP, addRandom);
        JSONObject memberObj = new JSONObject();
        memberObj.put("GroupId", groupId);
        log.info("腾讯云im解散群，请求参数：{}", memberObj);
        String addMemberResult = HttpUtil.doPost2(addHttpsUrl, memberObj);
        log.info("腾讯云im解散群，返回结果：{}", addMemberResult);
        JSONObject resultJson = JSONObject.parseObject(addMemberResult);
        return resultJson.getString("ActionStatus");
    }
    /**
     * 查询用户是否在直播群中
     * @param groupId
     * @param userIds
     * @return
     */
    public String checkUserInGroup(String groupId,List<String> userIds){
        //            添加群成员
        Integer addRandom = RandomUtils.nextInt(0, 999999999);
        String addHttpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.CHECK_MEMBERS, addRandom);
        JSONObject memberObj = new JSONObject();
        memberObj.put("GroupId", groupId);

        List<JSONObject> userList = new ArrayList<>();
        for (String userId : userIds) {
            JSONObject userObj = new JSONObject();
            userObj.put("Member_Account", userId);
            userList.add(userObj);
        }
        memberObj.put("MemberList", userList);
        log.info("腾讯云im添加群成员，请求参数：{}", memberObj);
        String addMemberResult = HttpUtil.doPost2(addHttpsUrl, memberObj);
        log.info("腾讯云im添加群成员，返回结果：{}", addMemberResult);
        JSONObject resultJson = JSONObject.parseObject(addMemberResult);
        return resultJson.getString("ActionStatus");
    }

    /**
     * 添加群成员
     * @param groupId
     * @param userIds
     * @return
     */
    public String addGroupMember(String groupId,List<String> userIds){
        //            添加群成员
        Integer addRandom = RandomUtils.nextInt(0, 999999999);
        String addHttpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.ADD_GROUP_MEMBER, addRandom);
        JSONObject memberObj = new JSONObject();
        memberObj.put("GroupId", groupId);

        List<JSONObject> userList = new ArrayList<>();
        for (String userId : userIds) {
            JSONObject userObj = new JSONObject();
            userObj.put("Member_Account", userId);
            userList.add(userObj);
        }
        memberObj.put("MemberList", userList);
        log.info("腾讯云im添加群成员，请求参数：{}", memberObj);
        String addMemberResult = HttpUtil.doPost2(addHttpsUrl, memberObj);
        log.info("腾讯云im添加群成员，返回结果：{}", addMemberResult);
        JSONObject resultJson = JSONObject.parseObject(addMemberResult);
        return resultJson.getString("ActionStatus");
    }

    /**
     * 删除群成员不通知其他成员
     *
     * @param userIds
     * @return
     */
    public String removeGroupMember(String groupId, List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.DELETE_GROUP_MEMBER, random);
        JSONObject obj = new JSONObject();
        obj.put("GroupId", groupId);
        obj.put("Silence", "1");
        obj.put("MemberToDel_Account", userIds);
        log.info("腾讯云im删除群成员，请求参数：{}", obj);
        String result = HttpUtil.doPost2(httpsUrl, obj);
        log.info("腾讯云im删除群成员，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return resultJson.getString("ActionStatus");
    }

    /**
     * 封禁群成员不通知其他成员
     *
     * @param groupId
     * @param second  取消禁言设置为0
     * @param userIds
     * @return
     */
    public String bannedGroupMember(String groupId, int second, List<String> userIds) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.FORBID_SEND_MSG, random);
        JSONObject obj = new JSONObject();
        obj.put("GroupId", groupId);
        obj.put("MuteTime", second);
        obj.put("Members_Account", userIds);
        log.info("批量禁言，请求参数：{}", obj);
        String result = HttpUtil.doPost2(httpsUrl, obj);
        log.info("批量禁言，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return resultJson.getString("ActionStatus");
    }
    /**
     * 指定人员发送群聊消息
     * @param fromUserId 发送方用户id
     * @param groupId 接收群id
     * @param msgType 消息对象类型
     * @param msgContent 消息内容
     */
    public String sendMsg(String fromUserId,String groupId, String msgType, String msgContent) {
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.BATCH_SEND_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("From_Account", fromUserId);
        jsonObject.put("GroupId", groupId);
        jsonObject.put("Random", random);
        List<JSONObject> msgBody = getMsgBody(msgType, msgContent);
        jsonObject.put("MsgBody", msgBody);
        log.info("腾讯云im发送群聊消息，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("腾讯云im发送群聊消息，返回结果：{}", result);
        return result;
    }

    /**
     * 拼接发送消息内容
     * @param msgType 消息类型
     * @param msgContent 发送消息内容
     * @return 消息内容
     */
    private List<JSONObject> getMsgBody(String msgType, String msgContent) {
        List<JSONObject> msgBody = new ArrayList<>();
        if (msgType.equals(TencentCloudImConstant.TIM_TEXT_ELEM)) {
            // 文本类型
            JSONObject msgBodyJson = new JSONObject();
            msgBodyJson.put("MsgType", msgType);
            JSONObject msgContentObj = new JSONObject();
            msgContentObj.put("Text", msgContent);
            msgBodyJson.put("MsgContent", msgContentObj);
            msgBody.add(msgBodyJson);
        }else if(msgType.equals(TencentCloudImConstant.TIM_CUSTOM_ELEM)){
            // 文本类型
            JSONObject msgBodyJson = new JSONObject();
            msgBodyJson.put("MsgType", msgType);
            JSONObject msgContentObj = new JSONObject();
            msgContentObj.put("Data", msgContent);
            msgContentObj.put("Desc", "notification");
            msgBodyJson.put("MsgContent", msgContentObj);
            msgBody.add(msgBodyJson);
        }
        return msgBody;
    }

    /**
     * 获取获取直播群在线人数
     * @param groupId
     * @return
     */
    public int getOnlineNumberByLiveGroup(String groupId){
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.GET_ONLINE_MEMBER_NUM, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("GroupId", groupId);
        log.info("获取直播群在线人数，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("获取直播群在线人数，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return (int)resultJson.get("OnlineMemberNum");
    }

    /**
     * 发送直播群消息
     * @param userId 发送消息用户id
     * @param groupId 直播群id
     * @param msgType 消息类型
     * @param content 消息内容
     * @return
     */
    public String sendLiveMsg(String userId,String groupId,String msgType,String content){
        //                    先检查用户是否导入IM
        List<String> userIds = new ArrayList<>();
        userIds.add(userId);
        Map<String, Boolean> resultMap = accountCheck(userIds);
        Boolean flag = resultMap.get(userId);
//        如果没有导入IM  则重新导入
        if (!flag) {
            H2AppUser h2AppUser = h2AppUserMapper.selectById(userId);
            accountImport(userId, h2AppUser.getNickName(), h2AppUser.getAvatar());
        }
//                    再检查用户是否在群里(默认直播群已经创建)
        String resultStatus = checkUserInGroup(groupId, userIds);
        if(StringUtils.equalsIgnoreCase(TencentCloudImConstant.ACTION_STATUS_FAIL,resultStatus)){
            addGroupMember(groupId,userIds);
        }
        return sendMsg(userId, groupId, msgType,content);
    }


    /**
     * 修改群信息
     *
     * @param groupId 群id
     * @param param 自定义参数
     * @param customField  是否是自定义信息
     * @return
     */
    public Boolean modifyGroupInfo(String groupId, Map<String, String> param, boolean customField){
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.MODIFY_GROUP_INFO, random);

        //修改基础/自定义字段
        JSONObject jsonObject = new JSONObject();
        if(customField){
            List<Map<String, String>> list = Lists.newArrayList();
            for (Map.Entry<String, String> entry : param.entrySet()) {
                HashMap<String, String> map = Maps.newHashMap();
                map.put("Key", entry.getKey());
                map.put("Value", entry.getValue());
                list.add(map);
            }
            jsonObject.put("AppDefinedData", list);
        } else {
            jsonObject = JSONObject.from(param);
        }
        jsonObject.put("GroupId", groupId);

        log.info("修改群信息，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("修改群信息，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(0 == (Integer) resultJson.get("ErrorCode")){
            return Boolean.TRUE;
        }else {
            return Boolean.FALSE;
        }
    }

    /**
     * 获取被禁言成员列表
     * @param groupId
     * @return
     */
    public List<String> getGroupMutedAccount(String groupId){
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.GET_GROUP_MUTED_ACCOUNT, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("GroupId", groupId);
        log.info("获取被禁言成员列表，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("获取被禁言成员列表，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(0 == (Integer) resultJson.get("ErrorCode")){
            JSONArray accountList = (JSONArray) resultJson.get("MutedAccountList");
            List<Map> list = accountList.toJavaList(Map.class);
            return list.stream().map(e -> (String) e.get("Member_Account")).collect(Collectors.toList());
        }else {
            return Lists.newArrayList();
        }
    }

    /**
     * 封禁群成员/踢人（仅支持直播群）
     * @param groupId 群id
     * @param userId 用户列表
     * @param time 封禁时间（单位秒）
     * @return
     */
    public boolean banGroupMember(String groupId, List<String> userId, Integer time){
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.BAN_GROUP_MEMBER, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("GroupId", groupId);
        jsonObject.put("Members_Account",userId);
        jsonObject.put("Duration", time);
        log.info("封禁群成员，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("封禁群成员，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return 0 == (Integer) resultJson.get("ErrorCode");
    }

    /**
     * 指定某人给某人发送消息
     * @param fromUser 发送者userId
     * @param toUser   接受者userId
     * @param message  消息内容（文本）
     * @return
     */
    public boolean specifySendMessage(String fromUser, String toUser, String message){
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.SEND_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SyncOtherMachine", TencentCloudImConstant.SYNC_OTHER_MACHINE_YES);
        jsonObject.put("From_Account", fromUser);
        jsonObject.put("To_Account", toUser);
        jsonObject.put("MsgRandom", random);
        jsonObject.put("MsgBody", getMsgBody(TencentCloudImConstant.TIM_TEXT_ELEM, message));
        log.info("指定发送消息，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("指定发送消息，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return 0 == (Integer) resultJson.get("ErrorCode");
    }

    /**
     * 在群组中发送普通消息
     * @param fromUser      发送者，可不指定默认为管理员
     * @param groupId       群组id
     * @param msgType       消息类型
     * @param msgContent    消息内容
     * @return
     */
    public boolean sendGroupMsg(String fromUser, String groupId, String msgType, String msgContent){
        H2AppUser h2AppUser = h2AppUserMapper.selectById(fromUser);
        Integer random = RandomUtils.nextInt(0, 999999999);
        String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.GroupManage.SEND_GROUP_MSG, random);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("GroupId", groupId);
        jsonObject.put("Random", random);
        jsonObject.put("MsgPriority", "High");
        jsonObject.put("MsgBody", getMsgBody(msgType, msgContent));
        if(StringUtils.isNotBlank(fromUser)){
            jsonObject.put("From_Account", fromUser);
            //先检查用户是否导入IM
            Map<String, Boolean> resultMap = accountCheck(Lists.newArrayList(fromUser));
            Boolean flag = resultMap.get(fromUser);
            //如果没有导入IM,则重新导入
            if (!flag) {
                accountImport(fromUser, h2AppUser.getNickName(), h2AppUser.getAvatar());
            }
        }
        log.info("在群组中发送普通消息，请求参数：{}", jsonObject);
        String result = HttpUtil.doPost2(httpsUrl, jsonObject);
        log.info("在群组中发送普通消息，返回结果：{}", result);
        JSONObject resultJson = JSONObject.parseObject(result);
        return 0 == (Integer) resultJson.get("ErrorCode");
    }

}
